package org.dromara.nursing.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.nursing.domain.vo.ContactVo;
import org.dromara.nursing.domain.bo.ContactBo;
import org.dromara.nursing.service.IContactService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 联系人信息
 *
 * @author Xue
 * @date 2025-03-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/nursing/contactInfo")
public class ContactController extends BaseController {

    private final IContactService contactService;

    /**
     * 查询联系人信息列表
     */
    // @SaCheckPermission("nursing:contactInfo:list") // 已移除权限验证
    @GetMapping("/list")
    public TableDataInfo<ContactVo> list(ContactBo bo, PageQuery pageQuery) {
        return contactService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出联系人信息列表
     */
    // @SaCheckPermission("nursing:contactInfo:export") // 已移除权限验证
    @Log(title = "联系人信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ContactBo bo, HttpServletResponse response) {
        List<ContactVo> list = contactService.queryList(bo);
        ExcelUtil.exportExcel(list, "联系人信息", ContactVo.class, response);
    }

    /**
     * 获取联系人信息详细信息
     *
     * @param contactId 主键
     */
    // @SaCheckPermission("nursing:contactInfo:query") // 已移除权限验证
    @GetMapping("/{contactId}")
    public R<ContactVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long contactId) {
        return R.ok(contactService.queryById(contactId));
    }

    /**
     * 新增联系人信息
     */
    // @SaCheckPermission("nursing:contactInfo:add") // 已移除权限验证
    @Log(title = "联系人信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ContactBo bo) {
        return toAjax(contactService.insertByBo(bo));
    }

    /**
     * 修改联系人信息
     */
    // @SaCheckPermission("nursing:contactInfo:edit") // 已移除权限验证
    @Log(title = "联系人信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ContactBo bo) {
        return toAjax(contactService.updateByBo(bo));
    }

    /**
     * 删除联系人信息
     *
     * @param contactIds 主键串
     */
    // @SaCheckPermission("nursing:contactInfo:remove") // 已移除权限验证
    @Log(title = "联系人信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{contactIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] contactIds) {
        return toAjax(contactService.deleteWithValidByIds(List.of(contactIds), true));
    }

    /**
     * 获取去重后的联系人列表（当前用户只显示一次）
     */
    @GetMapping("/uniqueList")
    public R<List<ContactVo>> uniqueList(ContactBo bo) {
        List<ContactVo> list = contactService.getUniqueContactList(bo);
        return R.ok(list);
    }

    /**
     * 验证老人身份证号
     *
     * @param elderId 老人ID
     * @param idCard 身份证号
     * @return 验证结果
     */
    @GetMapping("/verifyElder")
    public R<Boolean> verifyElder(@RequestParam Long elderId, @RequestParam String idCard) {
        // 使用ContactService验证老人身份证号
        return R.ok(contactService.verifyElderIdCard(elderId, idCard));
    }

    /**
     * 创建联系人与老人的关联关系
     *
     * @param contactId 联系人ID
     * @param elderId 老人ID
     * @return 是否创建成功
     */
    @Log(title = "创建联系人与老人关联", businessType = BusinessType.UPDATE)
    @PostMapping("/createRelation")
    public R<Boolean> createRelation(@RequestParam Long contactId, @RequestParam Long elderId) {
        return R.ok(contactService.createContactElderRelation(contactId, elderId));
    }
}
